package dao;

import entity.Items;
import util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by lirong
 * Date: 2017/6/10
 * Time: 22:53
 */
public class ItemsDAO {


    public ArrayList<Items> getAllItems(){

        Connection conn =null;
        PreparedStatement statement =null;
        ResultSet rs =null;

        ArrayList<Items> list =new ArrayList<Items>();

        try {
            conn = DBHelper.getConnection();
            String sql="select * from items";
            statement =conn.prepareStatement(sql);
            rs =statement.executeQuery();
            while (rs.next()){
                Items items =new Items();
                items.setId(rs.getInt("id"));
                items.setName(rs.getString("name"));
                items.setCity(rs.getString("city"));
                items.setPrice(rs.getInt("price"));
                items.setNumber(rs.getInt("number"));
                items.setPicture(rs.getString("picture"));
                list.add(items);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        }finally {
            if (rs!=null){
                try {
                    rs.close();
                    rs=null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (statement!=null){
                try {
                    statement.close();
                    statement=null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Items getItemsById(int id){

        Connection conn =null;
        PreparedStatement statement =null;
        ResultSet rs =null;

        try {
            conn =DBHelper.getConnection();
            String sql ="select * from items WHERE id=?";
            statement =conn.prepareStatement(sql);
            statement.setInt(1,id);
            rs =statement.executeQuery();
            if (rs.next()){
                Items items =new Items();
                items.setId(rs.getInt("id"));
                items.setName(rs.getString("name"));
                items.setCity(rs.getString("city"));
                items.setPrice(rs.getInt("price"));
                items.setNumber(rs.getInt("number"));
                items.setPicture(rs.getString("picture"));
                return items;
            }else {
                return null;
            }


        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            if (rs!=null){
                try {
                    rs.close();
                    rs=null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (statement!=null){
                try {
                    statement.close();
                    statement=null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public ArrayList<Items> getViewList(String list){

        ArrayList<Items> itemsList =new ArrayList<Items>();
        int iCount =5;
        if (list!=null && list.length()>0){
            String[] arr =list.split(",");
            if (arr.length>=5){
                for (int i = arr.length-1; i >=arr.length-iCount ; i--) {
                    itemsList.add(getItemsById(Integer.parseInt(arr[i])));
                }
            }else {
                for (int i = arr.length-1; i >=0 ; i--) {
                    itemsList.add(getItemsById(Integer.parseInt(arr[i])));
                }
            }
            return itemsList;
        }else {
            return null;
        }


    }


}
